//
//  UIButton+FTCache.m
//  FashionTech
//
//  Created by JackieYip on 16/8/9.
//  Copyright © 2016年 mondial-d. All rights reserved.
//

#import "UIButton+FTCache.h"
#import "UIButton+WebCache.h"

@implementation UIButton (FTCache)
- (void)ft_setImageWithURL:(NSURL *)URL forState:(UIControlState)state placeholderImage:(UIImage *)placeholderImage
{
    SDWebImageOptions options = SDWebImageLowPriority | SDWebImageRetryFailed;
    // 检查缓存
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    BOOL didCache = [manager cachedImageExistsForURL:URL];
    // 已缓存
    if (didCache) {
        NSString *key = [manager cacheKeyForURL:URL];
        UIImage *image = nil;
        image = [manager.imageCache imageFromMemoryCacheForKey:key];
        if (!image)
            image = [manager.imageCache imageFromDiskCacheForKey:key];
        [self setImage:image forState:state];
    }
    // 未缓存
    else {
        self.alpha = 0.0f;
        __weak typeof(self) weakImageView = self;
        [self sd_setImageWithURL:URL forState:state placeholderImage:placeholderImage options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            __strong typeof(weakImageView) strongImageView = weakImageView;
            [UIView animateWithDuration:0.5 animations:^{
                strongImageView.alpha = 1.0f;
            }];
        }];
    }
}
@end
